package com.yanqu.road.server.manager.activity.summerquiz;

import com.yanqu.road.dao.impl.activity.summerquiz.CrossSummerQuizDataDaoImpl;
import com.yanqu.road.dao.impl.activity.summerquiz.CrossSummerQuizUserDataDaoImpl;
import com.yanqu.road.dao.impl.activity.summerquiz.SummerQuizConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.summerquiz.CrossSummerQuizData;
import com.yanqu.road.entity.activity.summerquiz.CrossSummerQuizUserData;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizDetail;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.util.internal.ConcurrentSet;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSummerQuizActivity {

    private ActivityInfo activityInfo;

    private SummerQuizConfig config;

    private Map<Integer, Object> activityLockMap = new ConcurrentHashMap<>();

    //活动，玩家
    private Map<Integer, Map<Long, CrossSummerQuizUserData>> activityUserMap;

    //活动，quizId
    private Map<Integer, Map<Integer, CrossSummerQuizData>> activityQuizMap;

    private Set<CrossSummerQuizUserData> needUpdateUserDatMap = new ConcurrentSet<>();

    private Set<CrossSummerQuizData> needUpdateQuizDataMap = new ConcurrentSet<>();

    public void init() {

        initConfig();

        Map<Integer, Map<Long, CrossSummerQuizUserData>> activityUserMapTemp = new CrossSummerQuizUserDataDaoImpl().getCrossSummerQuizUserData(getActivityInfo().getActivityId());

        Map<Integer, Map<Integer, CrossSummerQuizData>> activityQuizMapTemp = new CrossSummerQuizDataDaoImpl().getCrossSummerQuizData(getActivityInfo().getActivityId());

        activityUserMap = activityUserMapTemp;

        activityQuizMap = activityQuizMapTemp;

    }

    public void initConfig(){

        SummerQuizConfig temp = new SummerQuizConfig(this.activityInfo);

        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());

        temp.setOddsRangeList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_RANGE.getName()).getValue(), "\\|"));
        temp.setSUMMERQUIZ_ODDS_CHANGE_TIME(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_CHANGE_TIME.getName()).getIntValue());
        temp.setOddsParamList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_PARAM.getName()).getValue(), "\\|"));

        temp.setSUMMERQUIZ_BULLIONS_ANTE(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_BULLIONS_ANTE.getName()).getIntValue());
        temp.setSUMMERQUIZ_LOSER_GET_BULLIONS(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_LOSER_GET_BULLIONS.getName()).getIntValue());
        SummerQuizConfigDaoImpl dao = new SummerQuizConfigDaoImpl();
        temp.setQuizInfoList(dao.getSummerQuizInfoList(temp.getActivityInfo().getActivityId()));

        config = temp;
    }

    public void saveData(){

        if(!needUpdateUserDatMap.isEmpty()){
            Set<CrossSummerQuizUserData> set;
            synchronized (needUpdateUserDatMap){
                set = new HashSet<>(needUpdateUserDatMap);
                needUpdateUserDatMap.clear();
            }
            for (CrossSummerQuizUserData userData : set) {
                if(userData.isInsertOption()){
                    new CrossSummerQuizUserDataDaoImpl().add(userData);
                }else if(userData.isUpdateOption()){
                    new CrossSummerQuizUserDataDaoImpl().update(userData);
                }
            }
        }

        if(!needUpdateQuizDataMap.isEmpty()){
            Set<CrossSummerQuizData> set;
            synchronized (needUpdateQuizDataMap){
                set = new HashSet<>(needUpdateQuizDataMap);
                needUpdateQuizDataMap.clear();
            }
            for (CrossSummerQuizData data : set) {
                if(data.isInsertOption()){
                    new CrossSummerQuizDataDaoImpl().add(data);
                }else if(data.isUpdateOption()){
                    new CrossSummerQuizDataDaoImpl().update(data);
                }
            }
        }
    }
    /**
     * 竞猜
     */
    public void userGuess(long serverId, long playerId, int gamesActivityId, int quizId, int type, int num, String consume) {

        CrossSummerQuizUserData userData = getUserData(gamesActivityId, playerId);
        if(userData.getServerId() == 0){
            userData.setServerId(serverId);
        }

        SummerQuizProto.CrossSummerQuizGuessActionRespMsg.Builder respMsg = SummerQuizProto.CrossSummerQuizGuessActionRespMsg.newBuilder();
        respMsg.setConsume(consume);
        SummerQuizProto.SummerQuizGuessActionRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessActionRespMsg.newBuilder();

        SummerQuizInfo summerQuizInfo = config.getSummerQuizInfo(quizId);
        if(summerQuizInfo == null){
            clientMsg.setRet(GameErrorCode.E_ERROR_DATA);
            respMsg.setClient(clientMsg);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_SUMMER_QUIZ_USER_GUESS_FROM_CROSS, respMsg));
            return;
        }

        CrossSummerQuizData quizData = getCrossSummerQuizData(gamesActivityId, quizId);

        Object lock = getLock(gamesActivityId);

        if(quizData.isInsertOption()) {
            synchronized (needUpdateQuizDataMap) {
                needUpdateQuizDataMap.add(quizData);
            }
        }

        SummerQuizDetail summerQuizDetail;
        synchronized (lock){

            summerQuizDetail = userData.getQuizDetailMap().get(quizId);
            if(summerQuizDetail != null){
                clientMsg.setRet(GameErrorCode.E_SUMMER_QUIZ_HAD_GUESS);
                respMsg.setClient(clientMsg);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_SUMMER_QUIZ_USER_GUESS_FROM_CROSS, respMsg));
                return;
            }

            summerQuizDetail = new SummerQuizDetail();
            summerQuizDetail.setNum(num);
            summerQuizDetail.setSelect(type);
            summerQuizDetail.setQuizId(quizId);
            if(type == 1){
                quizData.setYesSum(quizData.getYesSum() + num);
//                calQuizOdds(quizData, config, summerQuizInfo, true);
                summerQuizDetail.setOdds(quizData.getOddsYes());
                quizData.setYesCount(1 + quizData.getYesCount());
            }else if(type == 2){
                quizData.setNoSum(quizData.getNoSum() + num);
//                calQuizOdds(quizData, config, summerQuizInfo, false);
                summerQuizDetail.setOdds(quizData.getOddsNo());
                quizData.setNoCount(1 + quizData.getNoCount());
            }
            userData.getQuizDetailMap().put(quizId, summerQuizDetail);
            userData.setUpdateOption();
        }

        synchronized (needUpdateQuizDataMap) {
            needUpdateQuizDataMap.add(quizData);
        }

        synchronized (needUpdateUserDatMap){
            needUpdateUserDatMap.add(userData);
        }

        SummerQuizProto.SummerQuizDataTemp.Builder quizTemp = getQuizDataTempBuilder(quizData);
        clientMsg.setQuizHot(quizTemp);

        SummerQuizProto.SummerQuizUserQuizDetailTemp.Builder userQuizTemp = getUserQuizDetailBuilder(summerQuizDetail);
        clientMsg.setQuizDetail(userQuizTemp);
        clientMsg.setRet(0);

        respMsg.setClient(clientMsg);
        respMsg.setTitleId(quizId);
        respMsg.setSelectId(type);
        respMsg.setActivityId(activityInfo.getActivityId());
        respMsg.setGameActivityId(gamesActivityId);
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_SUMMER_QUIZ_USER_GUESS_FROM_CROSS, respMsg));

        userLogin(serverId, playerId, gamesActivityId);
    }

    public void settleGuess(long serverId, int gameActivityId, int quizId) {


    }

    /**
     * 领取竞猜奖励
     */
    public void getGuessReward(long serverId, long playerId, int gameActivityId, int quizId, int scoreGoodsId) {

        CrossSummerQuizUserData userData = getUserData(gameActivityId, playerId);
        if(userData.getServerId() == 0){
            userData.setServerId(serverId);
        }

        Object lock = getLock(gameActivityId);

        SummerQuizInfo summerQuizInfo = config.getSummerQuizInfo(quizId);
        if(summerQuizInfo == null || summerQuizInfo.getResult() == 0){
            //未到开奖时间
            SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
            clientMsg.setRet(GameErrorCode.E_SUMMER_QUIZ_NOT_IN_REWARD_TIME);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, clientMsg));
            return;
        }

        long getNum;
        SummerQuizDetail summerQuizDetail;
        synchronized (lock){

            summerQuizDetail = userData.getQuizDetailMap().get(quizId);
            if(summerQuizDetail == null || summerQuizDetail.getSelect() == 0){
                SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
                clientMsg.setRet(GameErrorCode.E_SUMMER_QUIZ_USER_NO_GUESS);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, clientMsg));
                //未参与竞猜
                return;
            }
            if(summerQuizDetail.isSettle()){
                SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
                clientMsg.setRet(GameErrorCode.E_SUMMER_QUIZ_HAD_GOT_GUESS_REWARD);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, clientMsg));
                return;
            }

//            if(summerQuizDetail.getSelect() != summerQuizInfo.getResult()){
//                //竞猜错误
//                SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
//                clientMsg.setRet(GameErrorCode.E_SUMMER_QUIZ_USER_GUESS_WRONG);
//                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, clientMsg));
//                return;
//            }

            CrossSummerQuizData quizData = getCrossSummerQuizData(userData.getGameActivityId(), summerQuizDetail.getQuizId());
            if(quizData == null){
                SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
                clientMsg.setRet(GameErrorCode.E_GAME_ERROR);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GUESS_GET_REWARD, clientMsg));
                return;
            }

            summerQuizDetail.setSettle(true);

            if(summerQuizDetail.getSelect() == summerQuizInfo.getResult()) {
                //计算奖励
                if(summerQuizDetail.getSelect() == 1) {
                    getNum = BigDecimal.valueOf(config.getSUMMERQUIZ_BULLIONS_ANTE()).multiply(BigDecimal.valueOf(quizData.getOddsYes())).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
                }else {
                    getNum = BigDecimal.valueOf(config.getSUMMERQUIZ_BULLIONS_ANTE()).multiply(BigDecimal.valueOf(quizData.getOddsNo())).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
                }
            }else {
                getNum = config.getSUMMERQUIZ_LOSER_GET_BULLIONS();
            }
            userData.setGuessScore(userData.getGuessScore() + getNum);
        }

        synchronized (needUpdateUserDatMap){
            needUpdateUserDatMap.add(userData);
        }

        SummerQuizProto.SummerQuizGuessGetRewardRespMsg.Builder serverMsg = SummerQuizProto.SummerQuizGuessGetRewardRespMsg.newBuilder();
        serverMsg.setRet(0);
        serverMsg.setReward(scoreGoodsId + "=" + getNum);
        serverMsg.setNum(getNum);
        serverMsg.setQuizDetail(getUserQuizDetailBuilder(summerQuizDetail));
        serverMsg.setGuessScore(userData.getGuessScore());
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_SUMMER_QUIZ_USER_GUESS_GET_REWARD_FROM_CROSS, serverMsg));
    }

    /**
     * 玩家登录
     */
    public void userLogin(long serverId, long playerId, int gameActivityId) {


        SummerQuizProto.SummerQuizUserDataSyncMsg.Builder clientMsg = SummerQuizProto.SummerQuizUserDataSyncMsg.newBuilder();

        //下发竞猜列表
        SummerQuizProto.SummerQuizUserDataTemp.Builder userDataBuilder = getUserDataBuilder(playerId, gameActivityId);
        clientMsg.setUserData(userDataBuilder);

        //竞猜热度
        List<SummerQuizProto.SummerQuizDataTemp> quizDataTempList = getSummerQuizDataTempList(gameActivityId);

        clientMsg.addAllQuizHot(quizDataTempList);

        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_SYNC, clientMsg));
    }

    public void getUserDataMsg(long serverId, long playerId, int gameActivityId) {

        SummerQuizProto.SummerQuizUserDataGetRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizUserDataGetRespMsg.newBuilder();
        clientMsg.setRet(0);

        //下发竞猜列表
        SummerQuizProto.SummerQuizUserDataTemp.Builder userDataBuilder = getUserDataBuilder(playerId, gameActivityId);
        clientMsg.setUserData(userDataBuilder);

        //竞猜热度
        List<SummerQuizProto.SummerQuizDataTemp> quizDataTempList = getSummerQuizDataTempList(gameActivityId);

        clientMsg.addAllQuizHot(quizDataTempList);

        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_USER_GET, clientMsg));
    }

    public void getQuizData(long serverId, long playerId, int gameActivityId) {

        SummerQuizProto.SummerQuizDataGetRespMsg.Builder clientMsg = SummerQuizProto.SummerQuizDataGetRespMsg.newBuilder();
        clientMsg.setRet(0);

        //竞猜热度
        List<SummerQuizProto.SummerQuizDataTemp> quizDataTempList = getSummerQuizDataTempList(gameActivityId);

        clientMsg.addAllQuizData(quizDataTempList);

        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_SUMMER_QUIZ_GUESS_HOT_GET, clientMsg));
    }

    /**
     * 计算赔率
     */
    public void calQuizOdds() {

        long now = System.currentTimeMillis();
        for (SummerQuizInfo quizInfo : config.getQuizInfoList()) {
            //判断时间
            if(!(quizInfo.getBeginGuizTime() < now && quizInfo.getEndGuizTime() > now)){
                continue;
            }
            long gap = config.getSUMMERQUIZ_ODDS_CHANGE_TIME() * DateHelper.SECOND_MILLIONS;
            for (Map.Entry<Integer, Map<Integer, CrossSummerQuizData>> entry : activityQuizMap.entrySet()) {
                int activityId = entry.getKey();
                Map<Integer, CrossSummerQuizData> dataMap = entry.getValue();
                CrossSummerQuizData quizData = dataMap.get(quizInfo.getId());
                if(quizData == null){
                    continue;
                }
                if(quizData.isSettle()){
                    continue;
                }
                if(quizData.getLastOddsTime() > 0 && now - quizData.getLastOddsTime() >= gap){
                    calQuizOdds(quizData, config, quizInfo, true);
                    calQuizOdds(quizData, config, quizInfo, false);
                    if(Config.isDebug()){
                        CrossSummerQuizMgr.getLogger().error("计算倍率  活动  {} ， 题目 {}", activityId, quizData.getQuizConfigId());
                    }
                    synchronized (needUpdateQuizDataMap) {
                        needUpdateQuizDataMap.add(quizData);
                    }
                }
            }
        }
    }

    private int calQuizOdds(CrossSummerQuizData quizData, SummerQuizConfig summerQuizConfig, SummerQuizInfo quizInfo, boolean yes) {
        int odds = 0;
        BigDecimal x;
        //有投票才有计算
        if(quizData.getYesCount() > 0 || quizData.getNoCount() > 0) {
            if (yes) {
                if (summerQuizConfig.getOddsParamList().size() != 4) {
                    return quizInfo.getInitialOdds().get(0);
                }
                x = BigDecimal.valueOf(quizData.getYesCount()).divide(BigDecimal.valueOf(quizData.getYesCount() + quizData.getNoCount()), 5, BigDecimal.ROUND_UP);
            } else {
                if (summerQuizConfig.getOddsParamList().size() != 4) {
                    return quizInfo.getInitialOdds().get(1);
                }
                x = BigDecimal.valueOf(quizData.getNoCount()).divide(BigDecimal.valueOf(quizData.getYesCount() + quizData.getNoCount()), 5, BigDecimal.ROUND_UP);
            }
            BigDecimal result = BigDecimal.valueOf(summerQuizConfig.getOddsParamList().get(0)).multiply(BigDecimal.valueOf(Math.pow(x.doubleValue(), 3)))
                    .add(BigDecimal.valueOf(summerQuizConfig.getOddsParamList().get(1)).multiply(BigDecimal.valueOf(Math.pow(x.doubleValue(), 2))))
                    .add(BigDecimal.valueOf(summerQuizConfig.getOddsParamList().get(2)).multiply(x))
                    .add(BigDecimal.valueOf(summerQuizConfig.getOddsParamList().get(3))).setScale(0, BigDecimal.ROUND_UP);
            odds = result.intValue();
        }
        if(odds < summerQuizConfig.getOddsRangeList().get(0)){
            odds = summerQuizConfig.getOddsRangeList().get(0);
        }
        if(odds > summerQuizConfig.getOddsParamList().get(1)){
            odds = summerQuizConfig.getOddsRangeList().get(1);
        }
        if(yes){
            quizData.setOddsYes(odds);
        }else {
            quizData.setOddsNo(odds);
        }
        quizData.setLastOddsTime(System.currentTimeMillis());
        return odds;
    }

    private Object getLock(int gamesActivityId) {
        Object lock = activityLockMap.get(gamesActivityId);
        if(lock == null){
            synchronized (activityLockMap){
                lock = activityLockMap.get(gamesActivityId);
                if(lock == null){
                    lock = new Object();
                    activityLockMap.put(gamesActivityId, lock);
                }
            }
        }
        return lock;
    }

    private CrossSummerQuizData getCrossSummerQuizData(int gamesActivityId, int quizId) {
        Map<Integer, CrossSummerQuizData> dataMap = activityQuizMap.get(gamesActivityId);
        if(dataMap == null){
            synchronized (activityQuizMap) {
                dataMap = activityQuizMap.get(gamesActivityId);
                if (dataMap == null) {
                    dataMap = new ConcurrentHashMap<>();
                    activityQuizMap.put(gamesActivityId, dataMap);
                }
            }
        }
        CrossSummerQuizData quizData = dataMap.get(quizId);
        if(quizData == null){
            synchronized (dataMap){
                quizData = dataMap.get(quizId);
                if(quizData == null){
                    quizData = new CrossSummerQuizData();
                    quizData.setActivityId(getActivityInfo().getActivityId());
                    quizData.setQuizConfigId(quizId);
                    quizData.setGameActivityId(gamesActivityId);
                    //实时赔率
                    SummerQuizInfo summerQuizInfo = config.getSummerQuizInfo(quizId);
                    quizData.setOddsYes(summerQuizInfo.getInitialOdds().get(0));
                    quizData.setOddsNo(summerQuizInfo.getInitialOdds().get(1));
                    quizData.setInsertOption();
                    quizData.setLastOddsTime(System.currentTimeMillis());
                    dataMap.put(quizId, quizData);
                }
            }
        }
        return quizData;
    }

    public CrossSummerQuizUserData getUserData(int gamesActivityId, long playerId) {
        Map<Long, CrossSummerQuizUserData> dataMap = activityUserMap.get(gamesActivityId);
        if(dataMap == null){
            synchronized (activityUserMap) {
                dataMap = activityUserMap.get(gamesActivityId);
                if (dataMap == null) {
                    dataMap = new ConcurrentHashMap<>();
                    activityUserMap.put(gamesActivityId, dataMap);
                }
            }
        }
        CrossSummerQuizUserData userData = dataMap.get(playerId);
        if(userData == null){
            synchronized (dataMap){
                userData = dataMap.get(playerId);
                if(userData == null){
                    userData = new CrossSummerQuizUserData();
                    userData.setActivityId(getActivityInfo().getActivityId());
                    userData.setUserId(playerId);
                    userData.setQuizDetailMap(new HashMap<>());
                    userData.setGameActivityId(gamesActivityId);
                    userData.setInsertOption();
                    dataMap.put(playerId, userData);
                }
            }
        }
        return userData;
    }

    public CrossSummerQuizUserData getUserDataIfNull(int gamesActivityId, long playerId) {
        Map<Long, CrossSummerQuizUserData> dataMap = activityUserMap.get(gamesActivityId);
        if(dataMap == null){
            return null;
        }
        return dataMap.get(playerId);
    }

    private List<SummerQuizProto.SummerQuizDataTemp> getSummerQuizDataTempList(int gameActivityId) {
        List<SummerQuizProto.SummerQuizDataTemp> quizDataTempList = new ArrayList<>();
        Map<Integer, CrossSummerQuizData> dataMap = activityQuizMap.get(gameActivityId);
        if(dataMap != null) {
            for (Map.Entry<Integer, CrossSummerQuizData> entry : dataMap.entrySet()) {
                CrossSummerQuizData quizData = entry.getValue();
                quizDataTempList.add(getQuizDataTempBuilder(quizData).build());
            }
        }
        return quizDataTempList;
    }

    private SummerQuizProto.SummerQuizUserDataTemp.Builder getUserDataBuilder(long playerId, int gameActivityId) {
        CrossSummerQuizUserData userDataIfNull = getUserDataIfNull(gameActivityId, playerId);
        SummerQuizProto.SummerQuizUserDataTemp.Builder userDataTemp = SummerQuizProto.SummerQuizUserDataTemp.newBuilder();
        userDataTemp.setGuessScore(0);
        if(userDataIfNull != null){
            for (Map.Entry<Integer, SummerQuizDetail> entry : userDataIfNull.getQuizDetailMap().entrySet()) {
                SummerQuizDetail quizDetail = entry.getValue();
                userDataTemp.addQuizDetail(getUserQuizDetailBuilder(quizDetail));
            }
        }
        return userDataTemp;
    }

    private SummerQuizProto.SummerQuizDataTemp.Builder getQuizDataTempBuilder(CrossSummerQuizData quizData) {
        SummerQuizProto.SummerQuizDataTemp.Builder quizTemp = SummerQuizProto.SummerQuizDataTemp.newBuilder();
        quizTemp.setId(quizData.getQuizConfigId());
        quizTemp.setYesCount(quizData.getYesCount());
        quizTemp.setNoCount(quizData.getNoCount());
        quizTemp.setOddsYes(quizData.getOddsYes());
        quizTemp.setOddsNo(quizData.getOddsNo());
        quizTemp.setYesSum(quizData.getYesSum());
        quizTemp.setNoSum(quizData.getNoSum());
        return quizTemp;
    }

    private SummerQuizProto.SummerQuizUserQuizDetailTemp.Builder getUserQuizDetailBuilder(SummerQuizDetail summerQuizDetail) {
        SummerQuizProto.SummerQuizUserQuizDetailTemp.Builder userQuizTemp = SummerQuizProto.SummerQuizUserQuizDetailTemp.newBuilder();
        userQuizTemp.setId(summerQuizDetail.getQuizId());
        userQuizTemp.setSelect(summerQuizDetail.getSelect());
        userQuizTemp.setNum(summerQuizDetail.getNum());
        userQuizTemp.setOdds(summerQuizDetail.getOdds());
        userQuizTemp.setIsGot(summerQuizDetail.isSettle());
        return userQuizTemp;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public SummerQuizConfig getConfig() {
        return config;
    }

    public CrossSummerQuizActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }
}
